# required parameters :
# 	$databaseName

properties {
	$projectName = "EtlDemo"
	$projectConfig = "Release"
	$base_dir = resolve-path .
	$source_dir = "$base_dir\src"
	$test_dir = "$base_dir\test"
	$package_dir = "$base_dir\package"
	$rhino_dir = "$package_dir\rhino"
	$ssis_dir = "$package_dir\ssis"
}

$framework = '3.5'

include ".\global_functions.ps1"

task default -depends Compile, Test, LoadData, Package

task TestRhino -depends RegenerateSchema, RunRhino, Assert { }
task PerfTestRhino -depends PerfTestData, RunRhino { }
task TestSsis -depends RegenerateSchema, RunSsis, Assert {}
task PerfTestSsis -depends PerfTestData, RunSsis  {}

task Init { 
    delete_directory $package_dir
    delete_directory $test_dir 
    create_directory $test_dir 
    create_directory $package_dir
	create_directory $rhino_dir
	create_directory $ssis_dir
	set-location $source_dir
	get-childitem * -include *.dll -recurse | remove-item
    get-childitem * -include *.pdb -recurse | remove-item
	get-childitem * -include *.exe -recurse | remove-item		
	set-location $base_dir
}

task Clean { 
    msbuild /t:clean /property:Configuration=$projectConfig /nologo /verbosity:m $source_dir\$projectName.sln
    delete_file $error_dir
}

task Compile -depends Init, Clean {
    exec {
    msbuild /t:build /v:m /nologo /p:Configuration=$projectConfig $source_dir\$projectName.sln
    } "Build failed - did not compile"
}

task CopyAssembliesForTest -depends Compile {
	copy_all_assemblies_for_test $test_dir
}

task RegenerateSchema -depends CopyAssembliesForTest {
	exec {
		& $base_dir\lib\nunit\nunit-console.exe $test_dir\$projectName.Tests.dll /nologo /nodots /include=Schema		
	}
}

task PerfTestData -depends RegenerateSchema {
	exec {	
		& "c:\Program Files (x86)\Red Gate\SQL Data Generator 1\SQLDataGenerator.exe" /project:$test_dir\test_data.sqlgen
	}
	exec {
		& "c:\Program Files (x86)\Red Gate\SQL Data Generator 1\SQLDataGenerator.exe" /project:$test_dir\test_dest_data.sqlgen
	}
}

task Assert -depends CopyAssembliesForTest {
	exec {
		& $base_dir\lib\nunit\nunit-console.exe $test_dir\$projectName.Tests.dll /nologo /nodots /include=Assert   
	} "Assertion failed"
}

task RunRhino -depends Package{
	exec { & $rhino_dir\Rhino.Etl.Cmd.exe -f:$rhino_dir\EtlDemo.Rhino.dll -p:JobCountProcess
	}
}

task RunSsis -depends Package {
	exec { & $ssis_dir\SsisRunner.exe }
}


task Package -depends Compile {
	Copy_and_flatten "$source_dir\rhino" *.exe $rhino_dir
	Copy_and_flatten "$source_dir\rhino" *.dll $rhino_dir
	Copy_and_flatten "$source_dir\rhino" *.config $rhino_dir
	Copy_and_flatten "$source_dir\ssisrunner" *.exe $ssis_dir
	Copy_and_flatten "$source_dir\ssisrunner" *.dll $ssis_dir
	Copy_and_flatten "$source_dir\ssisrunner" *.dtsx $ssis_dir
	Copy_and_flatten "$source_dir\ssisrunner" *.config $ssis_dir
}

function global:copy_all_assemblies_for_test($destination){
  create_directory $destination
  Copy_and_flatten $source_dir *.exe $destination
  Copy_and_flatten $source_dir *.dll $destination
  Copy_and_flatten $source_dir *.config $destination
  Copy_and_flatten $source_dir *.xml $destination
  Copy_and_flatten $source_dir *.pdb $destination
  Copy_and_flatten $source_dir *.xlsx $destination
  Copy_and_flatten $source_dir *.sqlgen $destination
  
  copy_files $source_dir\tests\Source $destination\Source
  copy_files $source_dir\tests\Destination $destination\Destination

  }

